package org.codegallery.javagal.thread;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

public class LoggingThread {
	private volatile boolean terminateRequested=false;
	private BlockingQueue<String> linesToLog = new LinkedBlockingQueue<String>();

	public void run() {
		try {
			while (!terminateRequested) {
				String line = linesToLog.take();
				doLogLine(line);
			}
		} catch (InterruptedException ex) {
			Thread.currentThread().interrupt();
		}
	}
	
	private void doLogLine(String line) {
		// ... write to wherever
	}

	public void log(String line) throws InterruptedException{
		linesToLog.put(line);
	}
	
	public void requestTerminate(){
		terminateRequested=true;
	}
}
